package com.rose.doorto.footbathdoor.web;

import com.rose.doorto.common.constant.Constant;
import com.rose.doorto.footbathdoor.pojo.Order;
import com.rose.doorto.footbathdoor.service.OrderService;
import com.rose.doorto.footbathdoor.service.impl.OrderServiceImpl;
import com.rose.doorto.footbathdoor.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 李庆超
 * @email 1481791489@qq.com
 * @Package：com.rose.doorto.footbathdoor.web
 * @Project：door-to-door
 * @name：OrderWebController
 * @date 2024/08/15 2024/8/15:10:24:
 */
@Controller
//@RequestMapping("/orderWeb")
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderServiceImpl orderServiceImpl;

    /**
     * 跳转支付页
     * @param orderSn
     * @param model
     * @return
     */
    @GetMapping("/topay")
    public String topay(@RequestParam("orderSn") String orderSn,
                        Model model) {
        Order order = orderService.getOrderByOrderSn(orderSn);
//        OrderCreateVO resp = order.getOrderDetail(orderSn);
        model.addAttribute("orderCreateVO", order);
        return "pay.html";
    }

    /**
     * 跳转去订单列表页
     * @param session
     * @param model
     * @return
     */
//    @GetMapping("/toList")
//    @GetMapping("/center/list.html")
//    public String toList(HttpSession session,
//                        Model model) {
//        User attribute = (User) session.getAttribute(AuthServerConstant.LOGIN_USER_KEY);
//        if (attribute!=null){
//            List<Order> list = orderService.findAllByUserId(attribute.getId().toString());
//            model.addAttribute("orderList",list);
//            return "list";
////            return "/list.html";
//        }
//        return null;
//
//    }

    /**
     * 用户订单列表
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/center/list.html")
    public String orderList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") String pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "3") String pageSize,
                            Model model,
                            HttpSession session) {
        Map<String, Object> params = new HashMap<>();
        params.put(Constant.PAGE, pageNum);
        params.put(Constant.LIMIT, pageSize);
        PageUtils page = orderService.getCurrentUserOrderList(params,session);
        model.addAttribute("page", page);
        return "list";
    }

    /**
     * 技师订单列表
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/center/orderIndex.html")
    public String orderAllList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") String pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "3") String pageSize,
                            Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put(Constant.PAGE, pageNum);
        params.put(Constant.LIMIT, pageSize);
        PageUtils page = orderService.getOrderList(params);
        List<Order> list = (List<Order>) page.getList();
        for (Order order : list) {
            List<Order> pageList = (List<Order>) page.getList();
            List<Order> orders = orderServiceImpl.hidePhoneNumber(order.getPhone());
            for (Order orderp : orders) {
                for (Order order1 : pageList) {
                    order1.setPhone(orderp.getPhone());
                }
            }
            page.setList(pageList);
        }
        model.addAttribute("page", page);
        return "orderIndex";
    }

}
